﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace T6task3
{
    class DBHelper
    {
        public static string SqlConn = "server=212.129.237.102;database=StuDB;uid=sa;pwd=Qq123456";
        public static SqlConnection conn = null;
        public static void connect()
        {
            //
            if (conn==null)
            {
                conn = new SqlConnection(SqlConn);
            }
            if (conn.State==ConnectionState.Closed)
            {
                conn.Open();
            }
            if (conn.State==ConnectionState.Broken)
            {
                conn.Close();
                conn.Open();
            }
        }
        //查询
        public static SqlDataReader Selectsql(string Sql)
        {
            connect();
            SqlCommand cmd = new SqlCommand(Sql,conn);
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        //增删改
        public static bool insertSql(string Sql)
        {
            connect();
            SqlCommand cmd = new SqlCommand(Sql,conn);
            int i = cmd.ExecuteNonQuery();
            conn.Close();
            return i > 0;
        }
        //聚合函数
        public static object Selectsum(string Sql)
        {
            connect();
            SqlCommand cmd = new SqlCommand(Sql,conn) ;
            object result = cmd.ExecuteScalar();
            conn.Close();
            return result;
        }
    }
}
